package com.nsu.tly.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nsu.tly.entity.Goods;
import com.nsu.tly.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Description:
 * @Author: Elder Tang
 * @Date: 2021/7/22 19:29
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/showGoods")
    public String showGoods(Model model, HttpServletResponse response, HttpServletRequest request) {
        List<Goods> goodsList=goodsService.showGoods();
        model.addAttribute("gList", goodsList);
        //System.out.println(goodsList);
        return "client/goodsList";
    }


    @RequestMapping("/listGoods")
    public String listGoods(@RequestParam(value = "page", defaultValue = "1") Integer page,Model model, HttpServletResponse response, HttpServletRequest request) {

        //获取指定页数据，大小为8
        PageHelper.startPage(page, 5);
        //紧跟的第一个select方法被分页
        List<Goods> goodsList=goodsService.showGoods();
        //使用PageInfo包装数据
        PageInfo pageInfo = new PageInfo(goodsList, 5);
        model.addAttribute("pageInfo", pageInfo);

        return "admin/goods/list";
    }


    @RequestMapping("/add")
    public void add(Goods goods, @RequestParam("goodsHomepic1") MultipartFile goodsHomepic,
                      @RequestParam("goodsCarouselpics1") List<MultipartFile> goodsCarouselpics,
                      HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 判断所上传文件是否存在
        if (!goodsHomepic.isEmpty()) {
            // 获取上传文件的原始名称
            String originalFilename1 = goodsHomepic.getOriginalFilename();
            // 设置上传文件的保存地址目录
            String dirPath1 = "D:\\local_img";
            File filePath = new File(dirPath1);
            // 如果保存文件的地址不存在，就先创建目录
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            // 使用UUID重新命名上传的文件名称(上传人_uuid_原始文件名称)
            String newFilename = UUID.randomUUID() + "_" + originalFilename1;
            try {
                // 使用MultipartFile接口的方法完成文件上传到指定位置
                String savePath = dirPath1 + File.separator + newFilename;
                goodsHomepic.transferTo(new File(savePath));
                String accessPath = "/local_img/" + newFilename;
                goods.setGoodsHomepic(accessPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        // 判断所上传文件是否存在
        if (!goodsCarouselpics.isEmpty() && goodsCarouselpics.size() > 0) {
            //循环输出上传的文件
            StringBuffer picList = new StringBuffer();
            for (MultipartFile file : goodsCarouselpics) {
                // 获取上传文件的原始名称
                String originalFilename = file.getOriginalFilename();
                // 设置上传文件的保存地址目录
                String dirPath = "D:\\local_img";
                File filePath = new File(dirPath);
                // 如果保存文件的地址不存在，就先创建目录
                if (!filePath.exists()) {
                    filePath.mkdirs();
                }
                // 使用UUID重新命名上传的文件名称(上传人_uuid_原始文件名称)
                String newFilename = UUID.randomUUID() + "_" + originalFilename;
                try {
                    // 使用MultipartFile接口的方法完成文件上传到指定位置
                    String savePath = dirPath + File.separator + newFilename;
                    file.transferTo(new File(savePath));
                    String accessPath = "/local_img/" + newFilename;
                    picList.append(accessPath);
                    picList.append(";");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            goods.setGoodsCarouselpics(picList.toString());
        }
        goodsService.addGoods(goods);
        request.getRequestDispatcher("/goods/listGoods").forward(request,response);
        //request.getRequestDispatcher("goods/listGoods").forward(request,response);
    }

}

